package edu.controller;

import edu.service.ScoreRatioService;
import edu.vo.ScoreRatio;
import edu.vo.ID.ScoreRatioId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/score-ratios")
public class ScoreRatioController {

    @Autowired
    private ScoreRatioService scoreRatioService;

    /**
     * 保存成绩比例信息
     * @param scoreRatio 成绩比例对象
     * @return 保存后的成绩比例对象
     */
    @PostMapping
    public ResponseEntity<ScoreRatio> saveScoreRatio(@RequestBody ScoreRatio scoreRatio) {
        ScoreRatio savedScoreRatio = scoreRatioService.saveScoreRatio(scoreRatio);
        return new ResponseEntity<>(savedScoreRatio, HttpStatus.CREATED);
    }

    /**
     * 根据 ID 获取成绩比例信息
     * @param id 成绩比例 ID
     * @return 成绩比例对象
     */
    @GetMapping("/{id}")
    public ResponseEntity<ScoreRatio> getScoreRatioById(@PathVariable ScoreRatioId id) {
        Optional<ScoreRatio> scoreRatioOptional = scoreRatioService.getScoreRatioById(id);
        return scoreRatioOptional.map(scoreRatio -> new ResponseEntity<>(scoreRatio, HttpStatus.OK))
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * 获取所有成绩比例信息
     * @return 成绩比例列表
     */
    @GetMapping
    public ResponseEntity<List<ScoreRatio>> getAllScoreRatios() {
        List<ScoreRatio> scoreRatios = scoreRatioService.getAllScoreRatios();
        return new ResponseEntity<>(scoreRatios, HttpStatus.OK);
    }

    /**
     * 根据 ID 删除成绩比例信息
     * @param id 成绩比例 ID
     * @return 无内容响应
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteScoreRatioById(@PathVariable ScoreRatioId id) {
        scoreRatioService.deleteScoreRatioById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 更新成绩比例信息
     * @param id 成绩比例 ID
     * @param scoreRatio 新的成绩比例信息
     * @return 更新后的成绩比例对象
     */
    @PutMapping("/{id}")
    public ResponseEntity<ScoreRatio> updateScoreRatio(@PathVariable ScoreRatioId id, @RequestBody ScoreRatio scoreRatio) {
        ScoreRatio updatedScoreRatio = scoreRatioService.updateScoreRatio(id, scoreRatio);
        return updatedScoreRatio != null ? new ResponseEntity<>(updatedScoreRatio, HttpStatus.OK)
                : new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
}